# ======================================================================
#
# Copyright (C) ADN
# ADN::SERVER::WebGet
# Description:
#  - Fonctionalite GET: CLOSE, EXIT, LOG
#  - Fonctionality GET: CLOSE, EXIT, LOG
#
# ======================================================================

package ADN::SERVER::WebGET;

use strict;
use ADN::LOG::Log;
use ADN::LANG::Lang;
use ADN::CONF::Path;
use ADN::CONF::Read;
use Proc::Background;
use ADN::SERVER::Header;
use ADN::SERVER::WebHTML;
use ADN::SERVER::Constant qw(:constant :log);
use ADN::SERVER::ParsePID;

# #######################
# DISPATCH WEB GET
# #######################
sub new
{
	  my $self = shift;
	  my $class = ref($self) || $self;
	  return $self if ref $self;
	  $self = bless {} => $class;
	  $self->{log} = ADN::LOG::Log->new();
		%{$self->{lang}} = ADN::LANG::Lang->new()->WebGET();
    $self->{header} = ADN::SERVER::Header->new();
    $self->{pid} = ADN::SERVER::ParsePID->new();
    return $self;
}

# #######################
# INITIALIZATION
# #######################
sub start
{
		my $self = shift;
		my ( $param, $header );
    %{$self->{param}} = @_;		
    if ( length $self->{param}{Param} < 5 ) {
			$header = $self->{header}->start( Data => '',	Nb => '200', Type => 'html' );
			$self->{param}{Nsock}->send( $header, '' );
			$self->{param}{Nsock}->close;
			undef $header;
			return;
    }
    $param = substr ( $self->{param}{Param}, 1, 4 );
    if ( $param eq 'main' ) {
			$self->my_45XX( 1 );
    }
    elsif ( $param eq 4200 ) {
			$self->my_4200( $self->{param}{Sock} );
    }
    elsif ( $param eq 'slog' ) {
			$self->my_45XX( 2 );
    }
    elsif ( $param eq 'proc' ) {
			$self->my_45XX( 3 );
    }
    elsif ( $param eq 'errs' ) {
			$self->my_errors();
    }
    elsif ( $param eq 'rest' ) {
			$self->my_rest();
    }
    undef $param;
    if (!close $self->{param}{Nsock}) {}
    return;
}

# #######################
# LOG & SEND LOG
# #######################
sub my_45XX {
		my $self = shift;
    my ( $data, $header, $slog, $type );
    $slog = $self->{log}->Log( 4500, 4500, 4500 );
    if ( $_[0] == 1 ) {
			$data = ADN::SERVER::WebHTML->new( Log => $slog, Opt => 1 );
			$type = 'html';
			$self->{log}->Log( $self->{lang}{mess1}, LOW, ALL );
    }
    elsif ( $_[0] == 2 ) {
			$data = ADN::SERVER::WebHTML->new( Log => $slog, Opt => 2 );
			$type = 'binary';
			$self->{log}->Log( $self->{lang}{mess2}, LOW, ALL );
    }
    elsif ( $_[0] == 3 ) {
			@{$self->{param}{Proc}} = $self->{pid}->start( Cmd => 'list', Proc => \@{$self->{param}{Proc}} );
			$data = ADN::SERVER::WebHTML->new( Log => \@{$self->{param}{Proc}}, Opt => 3 );
			$type = 'html';
			$self->{log}->Log( $self->{lang}{mess3}, LOW, ALL );
    }
    $header = $self->{header}->start(   Data => $data,
																				Nb => '200',
																				Type => $type );
    $self->{param}{Nsock}->send( $header, '' );
    undef *header;
    undef *data;
    return;
}

# #######################
# ERRORS JAVA & PASS
# #######################
sub my_errors {
		my $self = shift;
    my ( $data, $header );
    $data = ADN::SERVER::WebHTML->new( Opt => 4 );
    $header = $self->{header}->start(   Data => $data,
																				Nb => '200',
																				Type => 'html' );
    $self->{param}{Nsock}->send( $header, '' );
    undef *header;
    undef *data;
    return;
}

# #######################
# EXIT AND CLOSE SONDE
# #######################
sub my_4200 {
		my $self = shift;
    my ( $header );
    $self->{log}->Log( $self->{lang}{mess4}, LOW, ALL );
    $header = $self->{header}->start(   Data => '',
																				Nb => '200',
																				Type => 'html' );
    $self->{param}{Nsock}->send( $header, '' );
    $self->{param}{Sock}->close;
    undef *header;
    exit C_TRUE;
}

# #######################
# RESTART SONDE
# #######################
sub my_rest {
		my $self = shift;
    my ( $func, $kid, $exe );
    $self->my_45XX( 1 );
    $self->{log}->Log( $self->{lang}{mess5}, LOW, ALL );
    ( $exe , $func ) = ADN::SERVER::StartUP->folder();
    $func .= 'Res.pl';
    $kid = Proc::Background->new( $exe, $func );
    $kid = $kid->pid;
    if ( $kid == 0 ) {
			$self->{log}->Log( $self->{lang}{mess3}, HIGH, ALL );
			return;
    }
    undef $kid;
    return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__
